const done = true;
const p1 = new Promise((resolve, reject) => {
  setTimeout(() => {
    if (done) {
      resolve("ok");
    } else {
      reject("no ok");
    }
  }, 2000);
});
const p2 = new Promise((resolve, reject) => {
  if (done) {
    resolve("好");
  } else {
    reject("不好");
  }
});

const all = (arr) => {
  return new Promise((resolve, reject) => {
    // 先判断是否是函数
    if (!Array.isArray(arr)) {
      reject("请传入数组");
    }
    let result = [];
    // 成功的个数
    let count = 0;
    arr.forEach((item, index) => {
      // 每一个只要成功，那么将成功的结果放入数组中
      item
        .then((res) => {
          // result.push(res);
          result[index] = res;
          count++;
          if (arr.length === count) {
            resolve(result);
          }
        })
        .catch((err) => {
          reject(err);
        });
    });
  });
};

all([p1, p2])
  .then((res) => {
    console.log(res);
  })
  .catch((err) => {
    console.log(err);
  });
